#include "..\\include\Lib_Clara.h"
#include "..\\include\Dir.h"
#include "..\\include\cfg_items.h"
#include "main.h"
#include "config_data.h"
#include "board.cpp"
#include "conf_loader.h"
#define BOOKNAME "Image View"

int TerminateElf(void * ,BOOK* book)
{
  FreeBook(book);
  return(1);
}

void elf_exit(void)
{
  kill_data(&ELF_BEGIN, (void(*)(void*))mfree_adr());
}

int ShowAuthorInfo(void *mess ,BOOK* book)
{
 // MessageBox(0x6fFFFFFF,STR("Image View v1.5 \n\nBy Zylka\n"),0, 1 ,5000,0);
  wchar_t s[128];
  snwprintf(s,128,L"Image View v1.5 \n\nBy Zylka\n%s %s",__DATE__,__TIME__);
   MessageBox ( 0x6fFFFFFF, Str2ID(s,0,SID_ANY_LEN), 0, 1 , 5000, 0 );
  return(1);
}

int onAccept_DB(void * data, BOOK * book)
{
  RegisterImage(((FILEITEM*)data)->path,((FILEITEM*)data)->fname);
  wstrncpy(MMSBook->fname,((FILEITEM*)data)->fname,MAXELEMS(MMSBook->fname));
  wstrncpy(MMSBook->path,((FILEITEM*)data)->path,MAXELEMS(MMSBook->path));
  CreateBoardGui(MMSBook);
  ShowWindow(MMSBook->board);
  return(1);
}


int CreateDB(void *data, BOOK * book)
{
  MyBOOK *mbk=(MyBOOK *)book;
  void * DB_Desc=DataBrowserDesc_Create();
  const wchar_t * folder_list[2];
  folder_list[0]=GetDir(DIR_IMAGE | MEM_INTERNAL);
  folder_list[1]=GetDir(DIR_IMAGE | MEM_EXTERNAL);
  DataBrowserDesc_SetStyle(DB_Desc, STYL);
  int str_id;
  textidname2id(L"DB_PICTURES_TXT",SID_ANY_LEN,&str_id);
  DataBrowserDesc_SetHeaderText(DB_Desc,str_id);
  DataBrowserDesc_SetBookID(DB_Desc,BOOK_GetBookID(&mbk->book));
  DataBrowserDesc_SetFolders(DB_Desc,folder_list);
  DataBrowserDesc_SetFoldersNumber(DB_Desc,2);
  DataBrowserDesc_SetSelectAction(DB_Desc,1);
  DataBrowser_Create(DB_Desc);
  DataBrowserDesc_Destroy(DB_Desc);
  return(1);
}

static int onCancelDB(void *data, BOOK * book)
{
  FreeBook(book);
  return(1);
};


int isBook(BOOK * bk)
{
  if (!strcmp(bk->xbook->name,BOOKNAME)) return (1);
  return (0);
}

static int ReconfigElf(void *mess ,BOOK *book)
{
  RECONFIG_EVENT_DATA *reconf=(RECONFIG_EVENT_DATA *)mess;
  int result=0;
  if (wstrcmpi(reconf->path,successed_config_path)==0 && wstrcmpi(reconf->name,successed_config_name)==0)
  {
    InitConfig();
    result=1;
    info=IS_NAP;
    BookObj_Hide(FindBook(isBook),0);  //Stupid way to
    BookObj_Show(FindBook(isBook),0);  //force redraw
  }
  return(result);
}

int onBcfgConfig(void* mess,BOOK* b)
{
  FSTAT _fstat;
  wchar_t path[256];
  if(fstat(GetDir(DIR_ELFS|MEM_INTERNAL),L"BcfgEdit.elf",&_fstat)==0)
  {
    wstrcpy(path,GetDir(DIR_ELFS|MEM_INTERNAL));
  }
  else if(fstat(GetDir(DIR_ELFS|MEM_EXTERNAL),L"BcfgEdit.elf",&_fstat)==0)
  {
    wstrcpy(path,GetDir(DIR_ELFS|MEM_EXTERNAL));
  }
  else
  {
    MessageBox(0x6FFFFFFF, STR("BcfgEdit.elf not found!"), 0, 1 ,5000, 0);
    return (1);
  }
  wstrcat(path,L"/BcfgEdit.elf");
  elfload(path,(void*)successed_config_path,(void*)successed_config_name,0);
  return (1);
}

const PAGE_MSG bk_msglst_main[] @ "DYN_PAGE"  =
{
  PAGE_ENTER_EVENT_TAG,       CreateDB,
  ACCEPT_EVENT_TAG,           onAccept_DB,
  PREVIOUS_EVENT_TAG,         onCancelDB,
  CANCEL_EVENT_TAG,           onCancelDB,
  NIL_EVENT_TAG,              NULL
};

const PAGE_DESC bk_main = {"ImageView_main",0,bk_msglst_main};

const PAGE_MSG MS_PageEvents[]@ "DYN_PAGE" ={
  ELF_TERMINATE_EVENT,         TerminateElf,
  TERMINATE_SESSION_EVENT_TAG, TerminateElf,
  ELF_SHOW_INFO_EVENT,         ShowAuthorInfo,
  ELF_RECONFIG_EVENT,          ReconfigElf,
  ELF_BCFG_CONFIG_EVENT,       onBcfgConfig,
  NIL_EVENT_TAG,               NULL
};

PAGE_DESC base_page ={"MS_BasePage",0,MS_PageEvents};


void onCloseMSBook(BOOK * book)
{
  if (MMSBook->board)
  {
    unreg();
    for(int i=0;i<MMSBook->flist->FirstFree;i++)
    {
      TextFree(((FILE*)ListElement_GetByIndex(MMSBook->flist,i))->strname);
    }
    List_Free(MMSBook->flist);
    GUI_Free((GUI*)MMSBook->board);
  }
  SUBPROC(elf_exit);
}

int main(wchar_t *, wchar_t *path, wchar_t *fname)
{
  MyBOOK * MSBook= new MyBOOK;
  memset(MSBook,0,sizeof(MyBOOK));
  CreateBook(MSBook,onCloseMSBook,&base_page,BOOKNAME,-1,0);
  MMSBook=MSBook;
  InitConfig();
  info=IS_NAP;
  scr_w=Display_GetWidth(0), scr_h=Display_GetHeight(0);
  if (path && fname)
  {
    wstrncpy(MSBook->path,path,MAXELEMS(MSBook->path));
    wstrncpy(MSBook->fname,fname,MAXELEMS(MSBook->fname));
    MSBook->file_open=1;
    RegisterImage(path,fname);
    CreateBoardGui(MSBook);
    ShowWindow(MSBook->board);
  }
  else
  {
    BookObj_GotoPage((BOOK *)MSBook,&bk_main);
  }
  return 0;
}
